//
//  GDBDDatabaseUtils.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/2/12.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "GDBDDatabaseUtils.h"

@implementation GDBDDatabaseUtils

/**
 字典转JSON
 */
+ (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
/**
 JSON转字典
 */
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
    //json 字符串为空，返回nil
    if(jsonString == nil){
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if(error){
        NSLog(@"json解析失败:%@",error);
        return nil;
    }
    return dic;

}

/**
 根据存储的信息转为对应的变量类型
 */
+ (id)getIDVariableValueTypesWithString:(NSString *)string{
    NSString *idValueType = [[string componentsSeparatedByString:@":"] lastObject];
    NSString *idValue = [string stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@":%@",idValueType] withString:@""];
    if([idValueType isEqualToString:@"NSNumber"]){
        return [NSNumber numberWithInteger:[idValue integerValue]];
    }else if([idValueType isEqualToString:@"NSNumberChar"]){
        return [NSNumber numberWithChar:[idValue intValue]];
    }else if([idValueType isEqualToString:@"NSNumberFloat"]){
        return [NSNumber numberWithFloat:[idValue floatValue]];
    }else if([idValueType isEqualToString:@"NSNumberDouble"]){
        return [NSNumber numberWithDouble:[idValue doubleValue]];
    }else if([idValueType isEqualToString:@"NSNumberShort"]){
        return [NSNumber numberWithShort:[idValue intValue]];
    }else if([idValueType isEqualToString:@"NSNumberInt"]){
        return [NSNumber numberWithInt:[idValue intValue]];
    }else if([idValueType isEqualToString:@"NSNumberLong"]){
        return [NSNumber numberWithLong:[idValue floatValue]];
    }else if([idValueType isEqualToString:@"NSNumberLongLong"]){
        return [NSNumber numberWithLongLong:[idValue longLongValue]];
    }else if([idValueType isEqualToString:@"NSNumberNSInteger"]){
        return [NSNumber numberWithInteger:[idValue integerValue]];
    }else if([idValueType isEqualToString:@"NSNumberNSUInteger"]){
        return [NSNumber numberWithUnsignedInteger:[idValue integerValue]];
    }else if([idValueType isEqualToString:@"NSNumberBOOL"]){
        return [NSNumber numberWithBool:[idValue boolValue]];
    }else if([idValueType isEqualToString:@"UIView"]){
        return NSClassFromString(idValue);
    }else if([idValueType isEqualToString:@"NSString"]){
        return idValue;
    }
    return @"";
}
/**
 根据id变量类型转成为对应的string以供存储
 */
+ (NSString *)setIDVariableToString:(id)value{
    
    //NSString类型
    if([value isKindOfClass:[NSString class]]){
        return value?[NSString stringWithFormat:@"%@:NSString",value]:@"";
        //BOOL 类型
    }else if([[NSString stringWithFormat:@"%@",[value class]] isEqualToString:@"__NSCFBoolean"]){
        return value?[NSString stringWithFormat:@"%@:NSNumberBOOL",value]:@"";
        //基本数据类型
    }else if([value isKindOfClass:[NSNumber class]]){
        NSString *memberValueType = @":NSNumber";
        //通过比较类型字符串的长度判断类型
        if(strcmp([value objCType], @encode(char)) == 0 || strcmp([value objCType], @encode(unsigned char)) == 0){
            memberValueType = @"NSNumberChar";
        }else if(strcmp([value objCType], @encode(short)) == 0 ||strcmp([value objCType], @encode(unsigned short)) == 0){
            memberValueType = @"NSNumberShort";
        }else if(strcmp([value objCType], @encode(int)) == 0 || strcmp([value objCType], @encode(unsigned int)) == 0){
            memberValueType = @"NSNumberInt";
        }else if(strcmp([value objCType], @encode(long)) == 0 || strcmp([value objCType], @encode(unsigned long)) == 0){
            memberValueType = @"NSNumberLong";
        }else if(strcmp([value objCType], @encode(long long)) == 0 || strcmp([value objCType], @encode(unsigned long long)) == 0){
            memberValueType = @"NSNumberLongLong";
        }else if(strcmp([value objCType], @encode(float)) == 0){
            memberValueType = @"NSNumberFloat";
        }else if(strcmp([value objCType], @encode(double)) == 0){
            memberValueType = @"NSNumberDouble";
        }else if(strcmp([value objCType], @encode(NSInteger)) == 0){
            memberValueType = @"NSNumberNSInteger";
        }else if(strcmp([value objCType], @encode(NSUInteger)) == 0){
            memberValueType = @"NSNumberNSUInteger";
        }
        return value?[NSString stringWithFormat:@"%@%@",value,memberValueType]:@"";
        //UIView类型
    }else if([[value class] isSubclassOfClass:[UIView class]] || [[value class] isKindOfClass:[UIView class]]){
        return value?[NSString stringWithFormat:@"%@:UIView",value]:@"";
    }
    return value?[NSString stringWithFormat:@"%@:id",value]:@"";
}

@end

